package com.wzq.controller;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wzq.bean.MEMSdataModel;
import com.wzq.service.MemsdataService;
import com.wzq.util.ApiException;
import com.wzq.util.ApiResponse;
import com.wzq.util.Pager;
import com.wzq.util.ResponseCode;
import com.wzq.util.UtilDate;
import com.wzq.util.UtilString;

import net.sf.json.JSONObject;

@Controller 
@RequestMapping("MEMSdata/")
public class MEMSdataController{
	@Autowired
	private MemsdataService memsdataService;


	@RequestMapping(value="dataList",method=RequestMethod.POST)
	@ResponseBody
	public ApiResponse<List<MEMSdataModel>> dataList(MEMSdataModel model,HttpServletRequest request,Pager pager)throws ApiException{
		ApiResponse<List<MEMSdataModel>> response = new  ApiResponse<List<MEMSdataModel>>(ResponseCode.SUCC); 
		model.setMemstype("0");
		pager.setPageSize(500);
		List<MEMSdataModel> list1 = memsdataService.queryList(model, pager);//450条地形修正系数
		model.setMemstype("1");
		model.setEnable("Y");
		pager.setPageSize(45);
		List<MEMSdataModel> list2 = memsdataService.queryList(model, pager);//45条仪器记录
		
		Double asi=0.0d;
		Double ati=0.0d;
		DecimalFormat    df   = new DecimalFormat("######0.00"); 
		String	d1=null;
		String	d2=null;
		int num=0;
        for(int i=0;i<list2.size();i++){
         asi=UtilString.getAcceleration(list2.get(i).getPga());//峰值加速度场地修正系数
        for(int j=0;j<list1.size();j++){
        	if(j<=list1.size()-1){
    			d1=df.format(list1.get(j).getX());
    			d2=df.format(list2.get(j).getX());
        		if(d1.equals(d2)){
        			num=num+1;
        		}
        		d1=df.format(list1.get(j).getY());
    			d2=df.format(list2.get(j).getY());
    			if(d1.equals(d2)){
    				num=num+1;
        		}
        	}else{
        		ati=list1.get(j).getPga();
        		memsAi(list1, list2, asi, i, j);//算法
        	}
        	if(num==2){
        		memsAi(list1, list2, asi, i, j);//算法
        	}else{
        		num=0;
        	}
        	
        }
        }
		
		response.setData(list2);	
		return response;
	}

	//算法1
	private void memsAi(List<MEMSdataModel> list1, List<MEMSdataModel> list2, Double asi, int i, int j) {
		Double ati=0.0d;
		ati=list1.get(j).getPga();
		BigDecimal b=BigDecimal.valueOf(asi).multiply(BigDecimal.valueOf(ati));
		BigDecimal pga=BigDecimal.valueOf(list2.get(i).getPga()).divide(b);
		list2.get(i).setPga(pga.doubleValue());
	}

	@RequestMapping(value="MEMSImm",method=RequestMethod.POST)
	@ResponseBody
	public ApiResponse<List<MEMSdataModel>> MEMSImm(MEMSdataModel model,HttpServletRequest request,Pager pager)throws ApiException{
		ApiResponse<List<MEMSdataModel>> response = new  ApiResponse<List<MEMSdataModel>>(ResponseCode.SUCC); 
		model.setEnable("Y");
		model.setMemstype(null);
		pager.setPageSize(45);
		List<MEMSdataModel> list = memsdataService.queryList(model, pager);//45条仪器记录
		Double pga=0.0;
		BigDecimal bigd58=BigDecimal.ZERO;
		BigDecimal bigd59=BigDecimal.ZERO;
		BigDecimal bigd5=BigDecimal.ZERO;
		BigDecimal log10=BigDecimal.valueOf(Math.log(10.0d));	
		for(int i=0;i<list.size();i++){
			pga=list.get(i).getPga();
			if(null!=pga){
				bigd58=BigDecimal.valueOf(Math.log(pga)).divide(log10).multiply(BigDecimal.valueOf(3.66d)).subtract(BigDecimal.valueOf(Math.log(1.66d)));
				bigd5=BigDecimal.valueOf(Math.log(pga)).divide(log10).multiply(BigDecimal.valueOf(2.20d)).subtract(BigDecimal.valueOf(Math.log(1.00d)));
				bigd59=BigDecimal.valueOf(Math.log(pga)).divide(log10).multiply(BigDecimal.valueOf(3.47d)).subtract(BigDecimal.valueOf(Math.log(2.35d)));
				if(bigd58.compareTo(BigDecimal.valueOf(5d))==1&&bigd58.compareTo(BigDecimal.valueOf(8d))==-1){
					list.get(i).setImm(bigd58.doubleValue());
				}else{
					list.get(i).setImm(10d);
				}
				if(bigd5.compareTo(BigDecimal.valueOf(5d))==-1){
					list.get(i).setImm(bigd5.doubleValue());
				}else{
					list.get(i).setImm(10d);
				}
				if(bigd59.compareTo(BigDecimal.valueOf(5d))==1&&bigd59.compareTo(BigDecimal.valueOf(8d))==-1){
					if(bigd58.compareTo(bigd59)==-1){
						list.get(i).setImm(bigd58.doubleValue());
					}else{
						list.get(i).setImm(bigd59.doubleValue());
					}
				}else{
					list.get(i).setImm(10d);
				}
			}
		}
		
		response.setData(list);	
		return response;
	}

	
	@RequestMapping("add")
	@ResponseBody
	public ApiResponse<String> add(MEMSdataModel model,HttpServletRequest request)throws ApiException{
		ApiResponse<String> response = new  ApiResponse<String>(ResponseCode.SUCC);
		model.setEnable("N");
		memsdataService.updateBySelective(model);
		model.setEnable("Y");
		model.setMemstype("1");
		try {
			model.setServertime(UtilDate.DateToString(new Date()));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		memsdataService.add(model);
		return response;
	}
	
	@RequestMapping("update")
	@ResponseBody
	public ApiResponse<String> update(MEMSdataModel model,HttpServletRequest request)throws ApiException{
		ApiResponse<String> response = new  ApiResponse<String>(ResponseCode.SUCC);
		memsdataService.updateBySelective(model);
		return response;
	}
	
	@RequestMapping("delete")
	@ResponseBody
	public ApiResponse<String> delete(Integer id,HttpServletRequest request)throws ApiException{
		ApiResponse<String> response = new  ApiResponse<String>(ResponseCode.SUCC);
		memsdataService.delete(id);
		return response;
	}
	
	@RequestMapping("getById")
	@ResponseBody
	public ApiResponse<MEMSdataModel> getById(Integer id,HttpServletRequest request,Pager pager)throws ApiException{
		ApiResponse<MEMSdataModel> response = new  ApiResponse<MEMSdataModel>(ResponseCode.SUCC);       
		MEMSdataModel model = memsdataService.getById(id);
		response.setData(model);	
		return response;
	}
	
	
}
